package com.kevin.Code.Array;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @author Vinlee Xiao
 * @Classname LongestConsecutiveSequence
 * @Description Leetcode 128. 最长连续序列 高频 中等难度 没啥思路 参考题解
 * @Date 2021/11/18 19:41
 * @Version 1.0
 */
public class LongestConsecutiveSequence {
    public static void main(String[] args) {
        int[] nums = new int[]{100, 4, 200, 1, 3, 2};
        LongestConsecutiveSequence longestConsecutiveSequence = new LongestConsecutiveSequence();
        int i = longestConsecutiveSequence.longestConsecutive(nums);
        System.out.println(i);
    }


    /**
     * @param nums
     * @return
     */
    public int longestConsecutive(int[] nums) {

        int maxConsecutiveResult = 0;

        int len = nums.length;

        if (len == 0) {
            return 0;
        }

        HashSet<Integer> hashSet = new HashSet<>();
        //去重
        for (int num : nums) {
            hashSet.add(num);
        }


        for (Integer e : hashSet) {
            int currentLength = 0;
            if (!hashSet.contains(e - 1)) {
                int currentValue = e;
                while (hashSet.contains(currentValue)) {
                    currentLength++;
                    currentValue++;
                }

                if (currentLength > maxConsecutiveResult) {
                    maxConsecutiveResult = currentLength;
                }

            }
        }


        return maxConsecutiveResult;
    }
}
